package com.stu.thread.demo.thread_print;

import lombok.SneakyThrows;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Test {

    public static void main(String[] args) {

        //ThreadLocalRandom.current()
        //        .ints(1000,1,100)
        //        .forEach(a -> System.out.println(a));

        ThreadTask t1 = new ThreadTask();
        ThreadTask t2 = new ThreadTask();
        ThreadTask t3 = new ThreadTask();

        t1.setName("t1");
        t2.setName("t2");
        t3.setName("t3");

        t1.start();
        t2.start();
        t3.start();

    }



}

class ThreadTask extends Thread {

    public static final Integer END = 30;

    public BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();

    public BlockingQueue<Integer> getQueue() {
        return queue;
    }

    public ThreadTask next;

    @SneakyThrows
    @Override
    public void run() {
        while (true) {
            Integer take = queue.take();
            if (take >= END) {
                break;
            }
            Thread.sleep(Math.round(Math.random()* 1000));
            System.out.println(Thread.currentThread().getName() + "："+ (char) ('A' + take % 3));
            next.queue.put(take + 1);
        }
        if (next.isAlive()) {
            next.queue.put(END);
        }
    }
}
